<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Fund;

/**
 * FundSearch represents the model behind the search form about `common\models\Fund`.
 */
class FundSearch extends Fund
{
    public $user_username;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'relation_id1', 'relation_id2', 'fund_type'], 'integer'],
            [['deal_money', 'available_money'], 'number'],
            [['remark', 'create_time','user_username'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Fund::find();
        $query->joinWith(['user']);
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'fund.id' => $this->id,
            'user_id' => $this->user_id,
            'relation_id1' => $this->relation_id1,
            'relation_id2' => $this->relation_id2,
            'fund_type' => $this->fund_type,
            'deal_money' => $this->deal_money,
            'available_money' => $this->available_money,
            'create_time' => $this->create_time,
        ]);

        if ($this->user_username)
        {
            $query->andFilterWhere(['like', 'user.username', $this->user_username]);
        }

        $query->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }
}
